package com.xh.one2one.lesson.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xh.bussiness.practice.model.Practice;
import com.xh.bussiness.practice.service.IPracticeService;
import com.xh.bussiness.practice.service.IPracticeStudentService;
import com.xh.bussiness.practice.service.impl.PracticeStudentServiceImpl;
import com.xh.bussiness.resource.model.Material;
import com.xh.bussiness.resource.service.IMaterialService;
import com.xh.bussiness.tiku.bean.dto.QuestionDto;
import com.xh.bussiness.tiku.service.IPaperService;
import com.xh.bussiness.tiku.service.IQuestionService;
import com.xh.core.service.IBaseService;
import com.xh.core.util.DateUtils;
import com.xh.l.L;
import com.xh.one2one.lesson.bean.dto.LessonTimesAuditDto;
import com.xh.one2one.lesson.bean.dto.LessonTimesDto;
import com.xh.one2one.lesson.bean.request.*;
import com.xh.one2one.lesson.model.*;
import com.xh.one2one.lesson.dao.LessonTimesMapper;
import com.xh.one2one.lesson.service.*;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.one2one.teachHandout.bean.dto.HandoutDto;
import com.xh.one2one.teachHandout.bean.dto.HandoutNodeDto;
import com.xh.one2one.teachHandout.bean.request.TeachTimesHandoutRelationRequest;
import com.xh.one2one.teachHandout.model.HandoutContent;
import com.xh.one2one.teachHandout.model.HandoutNode;
import com.xh.one2one.teachHandout.model.TeachTimes;
import com.xh.one2one.teachHandout.model.TeachTimesHandoutRelation;
import com.xh.one2one.teachHandout.service.IHandoutContentService;
import com.xh.one2one.teachHandout.service.IHandoutNodeService;
import com.xh.one2one.teachHandout.service.ITeachTimesHandoutRelationService;
import com.xh.one2one.teachHandout.service.ITeachTimesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 备课第一步课次 服务实现类
 * </p>
 *
 * @author TW
 * @since 2021-04-09
 */
@Service
public class LessonTimesServiceImpl extends BaseServiceImpl<LessonTimesMapper, LessonTimes> implements ILessonTimesService {

    @Autowired
    private ITeachTimesService teachTimesService;
    @Autowired
    private ITeachTimesHandoutRelationService teachTimesHandoutRelationService;
    @Autowired
    private IMaterialService materialService;
    @Autowired
    private ILessonInfoService lessonInfoService;
    @Autowired
    private ILessonAuditService lessonAuditService;
    @Autowired
    private ILessonTimesAuditService lessonTimesAuditService;
    @Autowired
    private ILessonTimesHisService lessonTimesHisService;
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IHandoutNodeService handoutNodeService;
    @Autowired
    private ILessonEntranceExamService lessonEntranceExamService;
    @Autowired
    private ILessonHomeworkService lessonHomeworkService;
    @Autowired
    private IPracticeService practiceService;
    @Autowired
    private IPracticeStudentService practiceStudentService;
    @Autowired
    private IHandoutContentService contentService;
    @Autowired
    private IPaperService paperService;


    @Override
    public XhPage queryLessonTimesPage(XhPage page, LessonTimesRequest bean) {
        QueryWrapper qw = new QueryWrapper<LessonTimes>();
        //拼装条件
        qw = prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page, qw);
    }

    @Override
    public List queryLessonTimes(LessonTimesRequest bean) {
        QueryWrapper qw = new QueryWrapper<LessonTimes>();
        //拼装条件
        qw = prepareParams(bean, qw);
        if (!StringUtils.isEmpty(bean.getLessonId())) {
            qw.eq("lesson_id", bean.getLessonId());
        }
        if (!StringUtils.isEmpty(bean.getLessonThirdId())) {
            qw.eq("lesson_third_id", bean.getLessonThirdId());
        }
        if (!StringUtils.isEmpty(bean.getChecked())) {
            qw.eq("checked", bean.getChecked());
        }
        return this.dao.selectList(qw);
    }

    @Override
    public List queryAllLessonTimes(LessonTimesRequest bean) {
        QueryWrapper qw = new QueryWrapper<LessonTimes>();
        //拼装条件
        qw = prepareParams(bean, qw);
        if (!StringUtils.isEmpty(bean.getLessonId())) {
            qw.eq("lesson_id", bean.getLessonId());
        }
        LessonInfo lessonInfo = null;
        String classTime = null;
        if (!StringUtils.isEmpty(bean.getLessonThirdId())) {
            qw.eq("lesson_third_id", bean.getLessonThirdId());
            lessonInfo = lessonInfoService.getByThirdId(bean.getLessonThirdId());
            if (null != lessonInfo) {
                Date startTime = lessonInfo.getStartTime();
                Date endTime = lessonInfo.getEndTime();
                String startStr = DateUtils.format(startTime, "yyyy-MM-dd HH:mm");
                String endStr = DateUtils.format(endTime, "yyyy-MM-dd HH:mm");
                classTime = startStr + " -" + endStr.substring(10);
            }
        }
        qw.orderByAsc("sort_num");
        List<LessonTimesDto> list = this.selectList(qw, LessonTimesDto.class);
        if (!CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                LessonTimesDto lessonTimesDto = list.get(i);
                if (null != classTime) {
                    lessonTimesDto.setClassTime(classTime);
                }
                String id = lessonTimesDto.getId();
                TeachTimesHandoutRelationRequest relationRequest = new TeachTimesHandoutRelationRequest();
                relationRequest.setIndexId(id);
                List<TeachTimesHandoutRelation> timesHandoutRelation = teachTimesHandoutRelationService.queryList(relationRequest);

                if (!CollectionUtils.isEmpty(timesHandoutRelation)) {
                    TeachTimesHandoutRelation handoutRelation = timesHandoutRelation.get(0);
                    Integer type = handoutRelation.getType();
                    String handoutId = handoutRelation.getHandoutId();
                    HandoutDto handoutDto = new HandoutDto();
                    handoutDto.setHandoutId(handoutId);
                    handoutDto.setType(type);
                    handoutDto.setCopyHandout(handoutRelation.getCopyHandout());
                    Material material = materialService.getById(handoutId);
                    handoutDto.setName(material.getFileName());
                    if (1 == type) {
                        //type:1 查询上传的讲义，获取path
                        handoutDto.setPath(material.getFilePath());

                    } else {
                        //type:2,3  查询自建讲义，获取讲义的知识元，知识点内容
                        handoutDto.setChilds(teachTimesService.queryHandoutContent(handoutId));
                    }
                    lessonTimesDto.setChild(handoutDto);
                }
                //查询教学计划绑定的进门考
                LessonEntranceExamRequest examRequest = new LessonEntranceExamRequest();
                examRequest.setLessonTimeId(id);
                List<LessonEntranceExam> entranceExams = lessonEntranceExamService.queryLessonEntranceExam(examRequest);
                if(!CollectionUtils.isEmpty(entranceExams)){
                    LessonEntranceExam exam = entranceExams.get(0);
                    lessonTimesDto.setExam(paperService.getById(exam.getPaperId()));
                }
                //查询教学计划绑定的课堂巩固
                LessonHomeworkRequest homeworkRequest = new LessonHomeworkRequest();
                homeworkRequest.setLessonTimeId(id);
                List<LessonHomework> homeworkList = lessonHomeworkService.queryLessonHomework(homeworkRequest);
                if(!CollectionUtils.isEmpty(homeworkList)) {
                    LessonHomework homework = homeworkList.get(0);
                    lessonTimesDto.setHomework(paperService.getById(homework.getPaperId()));
                }
            }
        }
        return list;
    }

    @Override
    public void save(List<LessonTimesRequest> bean) {
        //先删除
//        this.dao.deleteByLessonId(bean.get(0));
        //再添加
        QueryWrapper qw = new QueryWrapper<LessonTimes>();
        qw.eq("lesson_id", bean.get(0).getLessonId());
        Integer count = this.selectCount(qw);
        for (int i = 0; i < bean.size(); i++) {
            LessonTimesRequest times = bean.get(i);
            LessonTimes lessonTimes = new LessonTimes();
            lessonTimes.setIndexName(times.getIndexName());
            lessonTimes.setPeriod(times.getPeriod());
            lessonTimes.setDifficultPoint(times.getDifficultPoint());
            lessonTimes.setSortNum(count + i + 1);
            lessonTimes.setLessonId(times.getLessonId());
            lessonTimes.setStatus(0);
            lessonTimes.setPlanId(times.getPlanId());
            this.insert(lessonTimes);

            String lessonTimesId = lessonTimes.getId();
            //绑定教学计划课次里的讲义
            TeachTimesHandoutRelationRequest relationRequest = new TeachTimesHandoutRelationRequest();
            relationRequest.setIndexId(times.getId());
            List<TeachTimesHandoutRelation> timesHandoutRelation = teachTimesHandoutRelationService.queryList(relationRequest);
            if (!CollectionUtils.isEmpty(timesHandoutRelation)) {
                TeachTimesHandoutRelation handoutRelation = timesHandoutRelation.get(0);
                Integer type = handoutRelation.getType();
                String handoutId = handoutRelation.getHandoutId();
                TeachTimesHandoutRelation newRelation = new TeachTimesHandoutRelation();
                newRelation.setIndexId(lessonTimesId);
                newRelation.setType(type);
                newRelation.setHandoutId(handoutId);
                teachTimesHandoutRelationService.insert(newRelation);
            }
            //查询教学计划绑定的进门考
            LessonEntranceExamRequest examRequest = new LessonEntranceExamRequest();
            examRequest.setLessonTimeId(times.getId());
            List<LessonEntranceExam> entranceExams = lessonEntranceExamService.queryLessonEntranceExam(examRequest);
            if(!CollectionUtils.isEmpty(entranceExams)){
                LessonEntranceExam exam = entranceExams.get(0);
                LessonEntranceExam entranceExam = new LessonEntranceExam();
                entranceExam.setLessonTimeId(lessonTimesId);
                entranceExam.setPaperId(exam.getPaperId());
                lessonEntranceExamService.insert(entranceExam);
            }
            //查询教学计划绑定的课堂巩固
            LessonHomeworkRequest homeworkRequest = new LessonHomeworkRequest();
            homeworkRequest.setLessonTimeId(times.getId());
            List<LessonHomework> homeworkList = lessonHomeworkService.queryLessonHomework(homeworkRequest);
            if(!CollectionUtils.isEmpty(homeworkList)){
                LessonHomework homework = homeworkList.get(0);
                LessonHomework lessonHomework = new LessonHomework();
                lessonHomework.setLessonTimeId(lessonTimesId);
                lessonHomework.setPaperId(homework.getPaperId());
                lessonHomeworkService.insert(lessonHomework);
            }
        }
    }

    @Override
    public LessonTimesAuditDto queryLessonAuditCalcu(LessonTimesRequest bean) {
        String lessonId = bean.getLessonId();
        LessonTimesRequest request = new LessonTimesRequest();
        request.setLessonId(lessonId);
        List<LessonTimes> lessonTimes = queryLessonTimes(request);
        if (CollectionUtils.isEmpty(lessonTimes)) {
            return null;
        }
        Integer periodCount = 0;
        Integer checkCount = 0;
        LessonTimesAuditDto auditDto = new LessonTimesAuditDto();

        for (int i = 0; i < lessonTimes.size(); i++) {
            LessonTimes lesson = lessonTimes.get(i);
            periodCount += lesson.getPeriod();
            checkCount += lesson.getStatus();
            if (bean.getId().equals(lesson.getId())) {
                auditDto.setIndexName(lesson.getIndexName());
                auditDto.setDifficultPoint(lesson.getDifficultPoint());
                auditDto.setPlanId(lesson.getPlanId());
            }
        }
        auditDto.setIndexCount(lessonTimes.size());
        auditDto.setPeriodCount(periodCount);
        auditDto.setChecked(checkCount);

        return auditDto;
    }

    @Override
    public void batchSaveForStudent(List<LessonTimesRequest> request) {
        String lessonId = request.get(0).getLessonId();
        doOldData(lessonId);

        for (int i = 0; i < request.size(); i++) {
            LessonTimesRequest lessonTimesRequest = request.get(i);
            LessonTimes lessonTimes = new LessonTimes();
            BeanUtils.copyProperties(lessonTimesRequest, lessonTimes);
            if (null == lessonTimesRequest.getId()) {
                this.insert(lessonTimes);
            } else {
                this.update(lessonTimes);
            }

            TeachTimesHandoutRelationRequest relationRequest = new TeachTimesHandoutRelationRequest();
            relationRequest.setIndexId(lessonTimesRequest.getId());
            List<TeachTimesHandoutRelation> list = teachTimesHandoutRelationService.queryList(relationRequest);
            String timesId = lessonTimes.getId();
            if (CollectionUtils.isEmpty(list)) {
                TeachTimesHandoutRelation teachTimesHandoutRelation = new TeachTimesHandoutRelation();
                teachTimesHandoutRelation.setIndexId(timesId);
                teachTimesHandoutRelation.setHandoutId(lessonTimesRequest.getHandoutId());
                teachTimesHandoutRelation.setType(lessonTimesRequest.getHandoutType());
                teachTimesHandoutRelationService.insert(teachTimesHandoutRelation);
            } else {
                TeachTimesHandoutRelation teachTimesHandoutRelation = list.get(0);
                teachTimesHandoutRelation.setHandoutId(lessonTimesRequest.getHandoutId());
                teachTimesHandoutRelation.setType(lessonTimesRequest.getHandoutType());
                teachTimesHandoutRelationService.update(teachTimesHandoutRelation);

            }

            //查询教学计划绑定的进门考
            LessonEntranceExamRequest examRequest = new LessonEntranceExamRequest();
            examRequest.setLessonTimeId(timesId);
            List<LessonEntranceExam> entranceExams = lessonEntranceExamService.queryLessonEntranceExam(examRequest);
            if(CollectionUtils.isEmpty(entranceExams)){
                LessonEntranceExam entranceExam = new LessonEntranceExam();
                entranceExam.setLessonTimeId(timesId);
                entranceExam.setPaperId(lessonTimesRequest.getExamId());
                lessonEntranceExamService.insert(entranceExam);
            }else{
                LessonEntranceExam exam = entranceExams.get(0);
                exam.setPaperId(lessonTimesRequest.getExamId());
                UpdateWrapper<LessonEntranceExam> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().set(LessonEntranceExam::getId,exam.getId());
                lessonEntranceExamService.update(exam, updateWrapper);
            }
            //查询教学计划绑定的课堂巩固
            LessonHomeworkRequest homeworkRequest = new LessonHomeworkRequest();
            homeworkRequest.setLessonTimeId(timesId);
            List<LessonHomework> homeworkList = lessonHomeworkService.queryLessonHomework(homeworkRequest);
            if(CollectionUtils.isEmpty(homeworkList)){
                LessonHomework lessonHomework = new LessonHomework();
                lessonHomework.setLessonTimeId(timesId);
                lessonHomework.setPaperId(lessonTimesRequest.getHomeworkId());
                lessonHomeworkService.insert(lessonHomework);
            }else{
                LessonHomework homework = homeworkList.get(0);
                homework.setPaperId(lessonTimesRequest.getHomeworkId());
                UpdateWrapper<LessonHomework> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().set(LessonHomework::getId,homework.getId());
                lessonHomeworkService.update(homework,updateWrapper);
            }
        }
    }

    private void doOldData(String lessonId) {
        Integer bindVersion = 1;
        LessonAuditRequest auditRequest = new LessonAuditRequest();
        auditRequest.setLessonId(lessonId);
        List<LessonAudit> lessonAudits = lessonAuditService.queryLessonAudit(auditRequest);
        if (!CollectionUtils.isEmpty(lessonAudits)) {
            bindVersion = lessonAudits.get(0).getBindVersion();
            for (int i = 0; i < lessonAudits.size(); i++) {
                LessonAudit audit = lessonAudits.get(i);
                audit.setBindVersion(bindVersion + 1);
            }
            lessonAuditService.batchUpdate(lessonAudits);

            LessonTimesAuditRequest lessonTimesAuditRequest = new LessonTimesAuditRequest();
            lessonTimesAuditRequest.setLessonId(lessonId);
            lessonTimesAuditRequest.setVersion(bindVersion);
            lessonTimesAuditService.updateByLessonId(lessonTimesAuditRequest);
        }

        LessonTimesRequest oldRequest = new LessonTimesRequest();
        oldRequest.setLessonId(lessonId);
        List<LessonTimes> oldTimes = queryLessonTimes(oldRequest);

        List<LessonTimesHis> list = new ArrayList<>();
        for (int i = 0; i < oldTimes.size(); i++) {
            LessonTimes lessonTimes = oldTimes.get(i);
            LessonTimesHis his = new LessonTimesHis();
            BeanUtils.copyProperties(lessonTimes, his);
            his.setVersion(bindVersion);
            his.setSubmitTime(lessonTimes.getModifyTime());

            TeachTimesHandoutRelationRequest relationRequest = new TeachTimesHandoutRelationRequest();
            relationRequest.setIndexId(lessonTimes.getId());
            List<TeachTimesHandoutRelation> timesHandoutRelation = teachTimesHandoutRelationService.queryList(relationRequest);
            if (!CollectionUtils.isEmpty(timesHandoutRelation)) {
                TeachTimesHandoutRelation handoutRelation = timesHandoutRelation.get(0);
                Integer type = handoutRelation.getType();
                String handoutId = handoutRelation.getHandoutId();
                his.setHandoutId(handoutId);
                his.setType(type);
            }
            his.setId(null);
            list.add(his);
        }
        if (list.size() > 0) {
            lessonTimesHisService.batchInsert(list);
        }
    }

    @Override
    public LessonTimesDto queryLessonInfoForStudent(LessonTimesRequest bean) {
        LessonTimesDto lessonTimesDto = new LessonTimesDto();
        QueryWrapper qw = new QueryWrapper<LessonTimes>();
        //拼装条件
        qw = prepareParams(bean, qw);
        if (!StringUtils.isEmpty(bean.getLessonId())) {
            qw.eq("lesson_id", bean.getLessonId());
        }
        LessonInfo lessonInfo = null;
        String classTime = null;
        if (!StringUtils.isEmpty(bean.getLessonThirdId())) {
            qw.eq("lesson_third_id", bean.getLessonThirdId());
            lessonInfo = lessonInfoService.getByThirdId(bean.getLessonThirdId());
            if (null != lessonInfo) {
                Date startTime = lessonInfo.getStartTime();
                Date endTime = lessonInfo.getEndTime();
                String startStr = DateUtils.format(startTime, "yyyy-MM-dd HH:mm");
                String endStr = DateUtils.format(endTime, "yyyy-MM-dd HH:mm");
                classTime = startStr + " -" + endStr.substring(10);
            }
        }
        List<TeachTimesHandoutRelation> timesHandoutRelation = getTimesHandoutRelations(lessonTimesDto, lessonInfo.getId(), bean.getLessonThirdId(), bean.getLessonId());

        if (!CollectionUtils.isEmpty(timesHandoutRelation)) {
            TeachTimesHandoutRelation handoutRelation = timesHandoutRelation.get(0);
            Integer type = handoutRelation.getType();
            String handoutId = handoutRelation.getHandoutId();
            HandoutDto handoutDto = new HandoutDto();
            handoutDto.setHandoutId(handoutId);
            handoutDto.setType(type);
            handoutDto.setCopyHandout(handoutRelation.getCopyHandout());
//            if(1 == type){
            //type:1 查询上传的讲义，获取path
            Material material = materialService.queryMaterialById(handoutId);
            handoutDto.setName(material.getFileName());
            handoutDto.setPath(material.getFilePath());

//            }else{
            //type:2,3  查询自建讲义，获取讲义的知识元，知识点内容
            handoutDto.setChilds(teachTimesService.queryHandoutContent(handoutId));
//            }
            lessonTimesDto.setChild(handoutDto);
        }
        lessonTimesDto.setClassTime(classTime);
        return lessonTimesDto;
    }

    @Override
    public List<TeachTimesHandoutRelation> getTimesHandoutRelations(LessonTimesDto dto, String infoId, String thirdId, String lessonId) {
        QueryWrapper qw = new QueryWrapper<LessonTimes>();
        qw.eq("lesson_id", lessonId);
        qw.eq("lesson_third_id", thirdId);
        LessonTimesDto lessonTimesDto = this.selectFirst(qw, LessonTimesDto.class);
        if (null != lessonTimesDto && null != dto) {
            BeanUtils.copyProperties(lessonTimesDto, dto);
        }
        List<TeachTimesHandoutRelation> timesHandoutRelation = null;
        TeachTimesHandoutRelationRequest relationRequest = new TeachTimesHandoutRelationRequest();
        if (null != lessonTimesDto) {
            String id = lessonTimesDto.getId();
            relationRequest.setIndexId(id);
            timesHandoutRelation = teachTimesHandoutRelationService.queryList(relationRequest);
        }

        if (CollectionUtils.isEmpty(timesHandoutRelation)) {
            relationRequest.setIndexId(infoId);
            timesHandoutRelation = teachTimesHandoutRelationService.queryList(relationRequest);
        }
        return timesHandoutRelation;
    }

    /**
     * 查询课程信息
     *
     * @param bean
     * @return com.xh.one2one.lesson.bean.dto.LessonTimesDto
     * @author 赵文博
     * @date 2021/6/21 13:49
     **/
    @Override
    public LessonTimesDto queryLessonInfoContent(LessonTimesRequest bean) {
        //查询上课时间
        LessonInfo lessonInfo = lessonInfoService.getByThirdId(bean.getLessonThirdId());
        String classTime = null;
        if (null != lessonInfo) {
            Date startTime = lessonInfo.getStartTime();
            Date endTime = lessonInfo.getEndTime();
            String startStr = DateUtils.format(startTime, "yyyy-MM-dd HH:mm");
            String endStr = DateUtils.format(endTime, "yyyy-MM-dd HH:mm");
            classTime = startStr + " -" + endStr.substring(10);
        }

        //查询直接上课的课次
        LessonTimesDto lessonTimesDto = queryNotPlan(lessonInfo.getId());
        if (lessonTimesDto == null) {
            lessonTimesDto = queryHavePlan(bean);
        }

        if (lessonTimesDto != null) {
            lessonTimesDto.setClassTime(classTime);
        }

        lessonTimesDto.setLessonThirdId(bean.getLessonThirdId());
        lessonTimesDto.setLessonId(bean.getLessonId());
        return lessonTimesDto;
    }

    @Override
    public void reLesson(LessonTimesRequest request) {

        String lessonTimesId = null;
        if(1 == request.getSourceType()){
            QueryWrapper qw=new QueryWrapper<LessonTimes>();
            qw.eq("lesson_id",request.getLessonId());
            qw.eq("lesson_third_id",request.getLessonThirdId());
            LessonTimes lessonTimes = this.selectFirst(qw);
            lessonTimes.setLessonThirdId(null);
            lessonTimes.setChecked(0);
            this.update(lessonTimes);
            lessonTimesId = lessonTimes.getId();
        }else{
            //删除讲义
            teachTimesHandoutRelationService.deleteByIndexId(request.getLessonId());
            lessonTimesId = request.getLessonId();
        }
        //删除进门考
        QueryWrapper entranceExamQueryWrapper=new QueryWrapper<LessonEntranceExam>();
        entranceExamQueryWrapper.eq("lesson_time_id",lessonTimesId);
        lessonEntranceExamService.deleteByCondition(entranceExamQueryWrapper);
        //删除作业
        QueryWrapper homeworkQueryWrappe = new QueryWrapper<LessonHomework>();
        homeworkQueryWrappe.eq("lesson_time_id",lessonTimesId);
        lessonHomeworkService.deleteByCondition(homeworkQueryWrappe);
        //更新备课
        LessonInfo lessonInfo = lessonInfoService.getByThirdId(request.getLessonThirdId());
        lessonInfo.setStep(1);
        lessonInfo.setStatus(0);
        lessonInfoService.update(lessonInfo);
        //删除已经发版的作业
        QueryWrapper practiceQueryWrapper = new QueryWrapper<Practice>();
        practiceQueryWrapper.eq("class_ids",lessonInfo.getId());
        Practice practice = practiceService.selectFirst(practiceQueryWrapper, Practice.class);
        if(null != practice){
            practiceStudentService.deleteByPracticeId(practice.getId());
            practiceService.deletePracticeById(practice.getId());
        }
    }

    /**
     * 准备条件
     *
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(LessonTimesRequest bean, QueryWrapper qw) {
        if (bean.getIds() != null && bean.getIds().length > 0) {
            qw.in("id", bean.getIds());
        }
        return qw;
    }

    /**
     * 获取绑定的讲义
     *
     * @param relation
     * @return com.xh.one2one.teachHandout.bean.dto.HandoutDto
     * @author 赵文博
     * @date 2021/6/21 11:49
     **/
    private HandoutDto getHandoutDto(TeachTimesHandoutRelation relation) {
        Integer type = relation.getType();
        String handoutId = relation.getHandoutId();
        HandoutDto handoutDto = new HandoutDto();
        handoutDto.setHandoutId(handoutId);
        handoutDto.setType(type);
        handoutDto.setCopyHandout(relation.getCopyHandout());
//        if (1 == type) {
            //type:1 查询上传的讲义，获取path
//            Material material = materialService.getById(handoutId);
            Material material = materialService.queryMaterialById(handoutId);
            handoutDto.setName(material.getFileName());
            handoutDto.setPath(material.getFilePath());
//        } else {
            if(type ==2 || type == 3){
                //type:2,3  查询自建讲义，获取讲义的知识元，知识点内容
                handoutDto.setChilds(teachTimesService.queryHandoutContent(handoutId));
            }
//        }
        return handoutDto;
    }

    /**
     * 获取没有教学计划的课程信息
     *
     * @param id
     * @return void
     * @author 赵文博
     * @date 2021/6/21 13:50
     **/
    private LessonTimesDto queryNotPlan(String id) {
        TeachTimesHandoutRelationRequest relationRequest = new TeachTimesHandoutRelationRequest();
        relationRequest.setIndexId(id);
        List<TeachTimesHandoutRelation> timesHandoutRelations = teachTimesHandoutRelationService.queryList(relationRequest);
        if (timesHandoutRelations.size() > 0) {
            HandoutDto handoutDto = getHandoutDto(timesHandoutRelations.get(0));
            LessonTimesDto lessonTimesDto = new LessonTimesDto();
            lessonTimesDto.setIndexName(handoutDto.getName());
            lessonTimesDto.setChild(handoutDto);
            lessonTimesDto.setId(id);
            return lessonTimesDto;
        }
        return null;
    }

    /**
     * 获取有教学计划的课程信息
     *
     * @param bean
     * @return com.xh.one2one.lesson.bean.dto.LessonTimesDto
     * @author 赵文博
     * @date 2021/6/21 13:51
     **/
    private LessonTimesDto queryHavePlan(LessonTimesRequest bean) {
        QueryWrapper qw = new QueryWrapper<LessonTimes>();
        //拼装条件
        qw = prepareParams(bean, qw);
        qw.eq("lesson_id", bean.getLessonId());
        qw.eq("lesson_third_id", bean.getLessonThirdId());
        qw.orderByAsc("sort_num");
        LessonTimesDto lessonTimesDto = this.selectFirst(qw, LessonTimesDto.class);

        TeachTimesHandoutRelationRequest relationRequest1 = new TeachTimesHandoutRelationRequest();
        relationRequest1.setIndexId(lessonTimesDto.getId());
        List<TeachTimesHandoutRelation> timesHandoutRelation1 = teachTimesHandoutRelationService.queryList(relationRequest1);

        if (!CollectionUtils.isEmpty(timesHandoutRelation1)) {
            HandoutDto handoutDto = getHandoutDto(timesHandoutRelation1.get(0));
            lessonTimesDto.setChild(handoutDto);
        }
        return lessonTimesDto;
    }

    /**
     * 查询课中里练习(在讲义里抽取)
     *
     * @param bean
     * @return java.util.List<com.xh.bussiness.tiku.bean.dto.QuestionDto>
     * @author 赵文博
     * @date 2021/6/21 15:41
     **/
    @Override
    public List<QuestionDto> getPracticeQuestion(LessonTimesRequest bean) {
        LessonInfo lessonInfo = lessonInfoService.getByThirdId(bean.getLessonThirdId());
        List<QuestionDto> questionDtoList = new ArrayList<>();
        if (lessonInfo != null) {
            QueryWrapper<TeachTimesHandoutRelation> qw = new QueryWrapper<>();
            qw.eq("index_id", lessonInfo.getId());
            TeachTimesHandoutRelation timesHandoutRelation = teachTimesHandoutRelationService.selectFirst(qw);
            if (timesHandoutRelation == null) {
                QueryWrapper qw1 = new QueryWrapper<LessonTimes>();
                qw1.eq("lesson_third_id", bean.getLessonThirdId());
                qw1.orderByAsc("sort_num");
                LessonTimesDto lessonTimesDto = this.selectFirst(qw1, LessonTimesDto.class);
                QueryWrapper<TeachTimesHandoutRelation> qw2 = new QueryWrapper<>();
                qw2.eq("index_id", lessonTimesDto.getId());
                timesHandoutRelation = teachTimesHandoutRelationService.selectFirst(qw2);
            }
            if (timesHandoutRelation != null) {
                QueryWrapper qwNode = new QueryWrapper<HandoutNode>();
                qwNode.eq("handout_id", timesHandoutRelation.getHandoutId());
//                qwNode.eq("type", 4);

                qwNode.orderByAsc("sort_num");
                List<HandoutNodeDto> handoutNodeDtoList = handoutNodeService.selectList(qwNode, HandoutNodeDto.class);
                for (HandoutNodeDto handoutNodeDto : handoutNodeDtoList) {
                    if(handoutNodeDto.getType()==3){
                        //知识元
                        getQuestionByKnowledge(questionDtoList,handoutNodeDto);
                    }else if(handoutNodeDto.getType()==4){
                        QuestionDto question = questionService.getQuestion(handoutNodeDto.getContent(), null, true, true, true, false, false);
                        if (question != null) {
                            questionDtoList.add(question);
                        }
                    }

                }
            }
        }
        return questionDtoList;
    }

    private void getQuestionByKnowledge(List<QuestionDto> questionDtoList,HandoutNodeDto handoutNodeDto){
        QueryWrapper<HandoutContent> contentQueryWrapper = new QueryWrapper<>();
        contentQueryWrapper.eq("node_id",handoutNodeDto.getId());
        contentQueryWrapper.eq("type",4);
        contentQueryWrapper.orderByAsc("sort_num");
        List<HandoutContent> contents = contentService.selectList(contentQueryWrapper,HandoutContent.class);
        for (HandoutContent content :contents){
            QuestionDto question = questionService.getQuestion(content.getContent(), null, true, true, true, false, false);
            if (question != null) {
                questionDtoList.add(question);
            }
        }
    }
}
